//
// Created by imagination on 4/1/25.
//
#include "setting_page.h"

#include "setting_basic_page.h"
#include "setting_factory_page.h"
#include "setting_display_page.h"
#include "setting_datetime_page.h"

#include "application.h"

#include "lv_i18n/lv_i18n.h"

#ifdef __cplusplus
extern "C" {
#endif

static lv_obj_t * right_container = NULL;
static lv_fragment_manager_t * fragment_manager = NULL;
static uint8_t current_index = 255;

static void button_clicked(lv_event_t * e)
{
    int id = (int) lv_event_get_user_data(e);

    if (current_index == id) {
        return;
    }

    current_index = id;

    switch (current_index) {
        case 1: {
            lv_fragment_t * fragment = lv_fragment_create(&setting_basic_fragment_cls, NULL);
            if (right_container) {
                lv_fragment_manager_replace(fragment_manager, fragment, &right_container);
            } else {
                lv_fragment_manager_push(app.manager, fragment, &app.container);
            }
            break; }
        case 2: {
            lv_fragment_t * fragment = lv_fragment_create(&setting_factory_fragment_cls, NULL);
            if (right_container) {
                lv_fragment_manager_replace(fragment_manager, fragment, &right_container);
            } else {
                lv_fragment_manager_push(app.manager, fragment, &app.container);
            }
            break; }
        case 4: {
            lv_fragment_t * fragment = lv_fragment_create(&setting_factory_fragment_cls, NULL);
            if (right_container) {
                lv_fragment_manager_replace(fragment_manager, fragment, &right_container);
            } else {
                lv_fragment_manager_push(app.manager, fragment, &app.container);
            }
            break; }
        case 5: {
            lv_fragment_t * fragment = lv_fragment_create(&setting_display_fragment_cls, NULL);
            if (right_container) {
                lv_fragment_manager_replace(fragment_manager, fragment, &right_container);
            } else {
                lv_fragment_manager_push(app.manager, fragment, &app.container);
            }
            break; }
        case 7: {
            lv_fragment_t * fragment = lv_fragment_create(&setting_datetime_fragment_cls, NULL);
            if (right_container) {
                lv_fragment_manager_replace(fragment_manager, fragment, &right_container);
            } else {
                lv_fragment_manager_push(app.manager, fragment, &app.container);
            }
            break; }
        default:
            break;
    }
}

static void draw_task_added(lv_event_t * e) {

}

static void container_delete_cb(lv_event_t * e) {
    lv_fragment_manager_t * manager = (lv_fragment_manager_t *) lv_event_get_user_data(e);
    lv_fragment_manager_delete(manager);
}

static void fragment_constructor_ctor(lv_fragment_t * self, void * args) {

}

static lv_obj_t * large_fragment_create_obj(lv_fragment_t * fragment, lv_obj_t * container) {
    lv_obj_t * root = lv_obj_create(container);
    lv_obj_set_layout(root, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(root, LV_FLEX_FLOW_ROW);
    lv_obj_set_size(root, LV_PCT(100), LV_PCT(100));

    lv_obj_t *list = lv_list_create(root);
    lv_obj_set_size(list, LV_PCT(30), LV_PCT(100));

    lv_obj_t * btn = NULL;
    lv_list_add_button(list, NULL, _("Vehicle"));
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Basic Setup"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)1);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Factory Set"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)2);

    lv_list_add_button(list, NULL, _("Equipment"));
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("EQ Setting"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)4);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Display Setting"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)5);

    lv_list_add_button(list, NULL, _("System"));
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Time Setting"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)7);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Background"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)8);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Version"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)9);

    fragment_manager = lv_fragment_manager_create(NULL);
    right_container = lv_obj_create(root);
    lv_obj_remove_style(right_container, NULL, LV_PART_MAIN);
    lv_obj_set_size(right_container, LV_PCT(70), LV_PCT(100));
    // lv_obj_add_event_cb(list, draw_task_added, LV_EVENT_DRAW_TASK_ADDED, NULL);
    // lv_obj_add_flag(list, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
    lv_obj_add_event_cb(root, container_delete_cb, LV_EVENT_DELETE, fragment_manager);

    return root;
}

static lv_obj_t * fragment_create_obj(lv_fragment_t * fragment, lv_obj_t * container)
{
    lv_obj_t *list = lv_list_create(container);
    lv_obj_set_size(list, LV_PCT(100), LV_PCT(100));

    lv_obj_t * btn = NULL;
    lv_list_add_button(list, NULL, _("Vehicle"));
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Basic Setup"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)1);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Factory Set"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)2);

    lv_list_add_button(list, NULL, _("Equipment"));
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("EQ Setting"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)4);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Display Setting"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)5);

    btn = lv_list_add_button(list, NULL, _("System"));
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Time Setting"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)7);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Background"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)8);
    btn = lv_list_add_button(list, LV_SYMBOL_OK, _("Version"));
    lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, (void*)9);

    // lv_obj_add_event_cb(list, draw_task_added, LV_EVENT_DRAW_TASK_ADDED, NULL);
    // lv_obj_add_flag(list, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);

    return list;
}

static bool fragment_event_cb(lv_fragment_t * self, int code, void * userdata) {
    if (code == 0) {
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 0), _("Vehicle"));
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 1), _("Basic Setup"));
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 2), _("Factory Set"));

        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 3), _("Equipment"));
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 4), _("EQ Setting"));
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 5), _("Display Setting"));

        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 6), _("System"));
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 7), _("Time Setting"));
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 8), _("Background"));
        lv_list_set_button_text(self->obj, lv_obj_get_child(self->obj, 9), _("Version"));
    }

    return false;
}

static bool large_fragment_event_cb(lv_fragment_t * self, int code, void * userdata) {
    if (code == 0) {
        lv_obj_t * list = lv_obj_get_child(self->obj, 0);

        lv_list_set_button_text(list, lv_obj_get_child(list, 0), _("Vehicle"));
        lv_list_set_button_text(list, lv_obj_get_child(list, 1), _("Basic Setup"));
        lv_list_set_button_text(list, lv_obj_get_child(list, 2), _("Factory Set"));

        lv_list_set_button_text(list, lv_obj_get_child(list, 3), _("Equipment"));
        lv_list_set_button_text(list, lv_obj_get_child(list, 4), _("EQ Setting"));
        lv_list_set_button_text(list, lv_obj_get_child(list, 5), _("Display Setting"));

        lv_list_set_button_text(list, lv_obj_get_child(list, 6), _("System"));
        lv_list_set_button_text(list, lv_obj_get_child(list, 7), _("Time Setting"));
        lv_list_set_button_text(list, lv_obj_get_child(list, 8), _("Background"));
        lv_list_set_button_text(list, lv_obj_get_child(list, 9), _("Version"));
    }

    return false;
}

lv_fragment_class_t setting_fragment_cls = {
    .constructor_cb = fragment_constructor_ctor,
    .create_obj_cb  = large_fragment_create_obj,
    .event_cb       = large_fragment_event_cb,
    .instance_size  = sizeof(lv_fragment_t),
};

void setting_page_init(void)
{
    switch (app.hor_res) {
        case 0 - 500:
            setting_fragment_cls.create_obj_cb = fragment_create_obj;
            break;
        default:
            setting_fragment_cls.create_obj_cb = large_fragment_create_obj;
            break;
    }
}

#ifdef __cplusplus
} /* extern "C" */
#endif
